home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / strings.c < prev    next >
C/C++ Source or Header  |  1994-10-14  |  12KB  |  518 lines

  1. /* strings -- print the strings of printable characters in files
  2.    Copyright (C) 1993, 94 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: strings [options] file...
  19.  
  20.    Options:
  21.    --all
  22.    -a
  23.    -        Do not scan only the initialized data section of object files.
  24.  
  25.    --print-file-name
  26.    -f        Print the name of the file before each string.
  27.  
  28.    --bytes=min-len
  29.    -n min-len
  30.    -min-len    Print graphic char sequences, MIN-LEN or more bytes long,
  31.         that are followed by a NUL or a newline.  Default is 4.
  32.  
  33.    --radix={o,x,d}
  34.    -t {o,x,d}    Print the offset within the file before each string,
  35.         in octal/hex/decimal.
  36.  
  37.    -o        Like -to.  (Some other implementations have -o like -to,
  38.         others like -td.  We chose one arbitrarily.)
  39.  
  40.    --target=BFDNAME
  41.         Specify a non-default object file format.
  42.  
  43.    --help
  44.    -h        Print the usage message on the standard output.
  45.  
  46.    --version
  47.    -v        Print the program version number.
  48.  
  49.    Written by Richard Stallman <rms@gnu.ai.mit.edu>
  50.    and David MacKenzie <djm@gnu.ai.mit.edu>.  */
  51.  
  52. #include <stdio.h>
  53. #include <getopt.h>
  54. #include <ctype.h>
  55. #include <errno.h>
  56. #include "bfd.h"
  57. #include "bucomm.h"
  58.  
  59. #ifdef isascii
  60. #define isgraphic(c) (isascii (c) && isprint (c))
  61. #else
  62. #define isgraphic(c) (isprint (c))
  63. #endif
  64.  
  65. #ifndef errno
  66. extern int errno;
  67. #endif
  68.  
  69. /* The BFD section flags that identify an initialized data section.  */
  70. #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS)
  71.  
  72. /* Radix for printing addresses (must be 8, 10 or 16).  */
  73. static int address_radix;
  74.  
  75. /* Minimum length of sequence of graphic chars to trigger output.  */
  76. static int string_min;
  77.  
  78. /* true means print address within file for each string.  */
  79. static boolean print_addresses;
  80.  
  81. /* true means print filename for each string.  */
  82. static boolean print_filenames;
  83.  
  84. /* true means for object files scan only the data section.  */
  85. static boolean datasection_only;
  86.  
  87. /* true if we found an initialized data section in the current file.  */
  88. static boolean got_a_section;
  89.  
  90. /* The BFD object file format.  */
  91. static char *target;
  92.  
  93. extern char *program_version;
  94.  
  95. static struct option long_options[] =
  96. {
  97.   {"all", no_argument, NULL, 'a'},
  98.   {"print-file-name", no_argument, NULL, 'f'},
  99.   {"bytes", required_argument, NULL, 'n'},
  100.   {"radix", required_argument, NULL, 't'},
  101.   {"target", required_argument, NULL, 'T'},
  102.   {"help", no_argument, NULL, 'h'},
  103.   {"version", no_argument, NULL, 'v'},
  104.   {NULL, 0, NULL, 0}
  105. };
  106.  
  107. static boolean strings_file PARAMS ((char *file));
  108. static int integer_arg PARAMS ((char *s));
  109. static void print_strings PARAMS ((char *filename, FILE *stream,
  110.                   file_ptr address, int stop_point,
  111.                   int magiccount, char *magic));
  112. static void usage PARAMS ((FILE *stream, int status));
  113.  
  114. void
  115. main (argc, argv)
  116.      int argc;
  117.      char **argv;
  118. {
  119.   int optc;
  120.   int exit_status = 0;
  121.   boolean files_given = false;
  122.  
  123.   program_name = argv[0];
  124.   xmalloc_set_program_name (program_name);
  125.   string_min = -1;
  126.   print_addresses = false;
  127.   print_filenames = false;
  128.   datasection_only = true;
  129.   target = NULL;
  130.  
  131.   while ((optc = getopt_long (argc, argv, "afn:ot:v0123456789",
  132.                   long_options, (int *) 0)) != EOF)
  133.     {
  134.       switch (optc)
  135.     {
  136.     case 'a':
  137.       datasection_only = false;
  138.       break;
  139.  
  140.     case 'f':
  141.       print_filenames = true;
  142.       break;
  143.  
  144.     case 'h':
  145.       usage (stdout, 0);
  146.  
  147.     case 'n':
  148.       string_min = integer_arg (optarg);
  149.       if (string_min < 1)
  150.         {
  151.           fprintf (stderr, "%s: invalid number %s\n",
  152.                program_name, optarg);
  153.           exit (1);
  154.         }
  155.       break;
  156.  
  157.     case 'o':
  158.       print_addresses = true;
  159.       address_radix = 8;
  160.       break;
  161.  
  162.     case 't':
  163.       print_addresses = true;
  164.       if (optarg[1] != '\0')
  165.         usage (stderr, 1);
  166.       switch (optarg[0])
  167.         {
  168.         case 'o':
  169.           address_radix = 8;
  170.           break;
  171.  
  172.         case 'd':
  173.           address_radix = 10;
  174.           break;
  175.  
  176.         case 'x':
  177.           address_radix = 16;
  178.           break;
  179.  
  180.         default:
  181.           usage (stderr, 1);
  182.         }
  183.       break;
  184.  
  185.     case 'T':
  186.       target = optarg;
  187.       break;
  188.  
  189.     case 'v':
  190.       printf ("GNU %s version %s\n", program_name, program_version);
  191.       exit (0);
  192.  
  193.     case '?':
  194.       usage (stderr, 1);
  195.  
  196.     default:
  197.       if (string_min < 0)
  198.         string_min = optc;
  199.       else
  200.         string_min = string_min * 10 + optc - '0';
  201.       break;
  202.     }
  203.     }
  204.  
  205.   if (string_min < 0)
  206.     string_min = 4;
  207.  
  208.   bfd_init ();
  209.  
  210.   for (; optind < argc; ++optind)
  211.     {
  212.       if (!strcmp (argv[optind], "-"))
  213.     datasection_only = false;
  214.       else
  215.     {
  216.       files_given = true;
  217.       exit_status |= (strings_file (argv[optind]) == false);
  218.     }
  219.     }
  220.  
  221.   if (files_given == false)
  222.     usage (stderr, 1);
  223.  
  224.   exit (exit_status);
  225. }
  226.  
  227. /* Scan section SECT of the file ABFD, whose printable name is FILE.
  228.    If it contains initialized data,
  229.    set `got_a_section' and print the strings in it.  */
  230.  
  231. static void
  232. strings_a_section (abfd, sect, file)
  233.      bfd *abfd;
  234.      asection *sect;
  235.      PTR file;
  236. {
  237.   if ((sect->flags & DATA_FLAGS) == DATA_FLAGS)
  238.     {
  239.       bfd_size_type sz = bfd_get_section_size_before_reloc (sect);
  240.       PTR mem = xmalloc (sz);
  241.       if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sz))
  242.     {
  243.       got_a_section = true;
  244.       print_strings (file, (FILE *) NULL, sect->filepos, 0, sz, mem);
  245.     }
  246.       free (mem);
  247.     }
  248. }
  249.  
  250. /* Scan all of the sections in FILE, and print the strings
  251.    in the initialized data section(s).
  252.  
  253.    Return true if successful,
  254.    false if not (such as if FILE is not an object file).  */
  255.  
  256. static boolean
  257. strings_object_file (file)
  258.      char *file;
  259. {
  260.   bfd *abfd = bfd_openr (file, target);
  261.  
  262.   if (abfd == NULL)
  263.     {
  264.       /* Treat the file as a non-object file.  */
  265.       return false;
  266.     }
  267.  
  268.   /* This call is mainly for its side effect of reading in the sections.
  269.      We follow the traditional behavior of `strings' in that we don't
  270.      complain if we don't recognize a file to be an object file.  */
  271.   if (bfd_check_format (abfd, bfd_object) == false)
  272.     {
  273.       bfd_close (abfd);
  274.       return false;
  275.     }
  276.  
  277.   got_a_section = false;
  278.   bfd_map_over_sections (abfd, strings_a_section, file);
  279.  
  280.   if (!bfd_close (abfd))
  281.     {
  282.       bfd_nonfatal (file);
  283.       return false;
  284.     }
  285.  
  286.   return got_a_section;
  287. }
  288.  
  289. /* Print the strings in FILE.  Return true if ok, false if an error occurs.  */
  290.  
  291. static boolean
  292. strings_file (file)
  293.      char *file;
  294. {
  295.   /* If we weren't told to scan the whole file,
  296.      try to open it as an object file and only look at
  297.      initialized data sections.  If that fails, fall back to the
  298.      whole file.  */
  299.   if (!datasection_only || !strings_object_file (file))
  300.     {
  301.       FILE *stream;
  302.  
  303.       stream = fopen (file, "rb");
  304.       /* Not all systems permit "rb", so try "r" if it failed.  */
  305.       if (stream == NULL)
  306.     stream = fopen (file, "r");
  307.       if (stream == NULL)
  308.     {
  309.       fprintf (stderr, "%s: ", program_name);
  310.       perror (file);
  311.       return false;
  312.     }
  313.  
  314.       print_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
  315.  
  316.       if (fclose (stream) == EOF)
  317.     {
  318.       fprintf (stderr, "%s: ", program_name);
  319.       perror (file);
  320.       return false;
  321.     }
  322.     }
  323.  
  324.   return true;
  325. }
  326.  
  327. /* Find the strings in file FILENAME, read from STREAM.
  328.    Assume that STREAM is positioned so that the next byte read
  329.    is at address ADDRESS in the file.
  330.    Stop reading at address STOP_POINT in the file, if nonzero.
  331.  
  332.    If STREAM is NULL, do not read from it.
  333.    The caller can supply a buffer of characters
  334.    to be processed before the data in STREAM.
  335.    MAGIC is the address of the buffer and
  336.    MAGICCOUNT is how many characters are in it.
  337.    Those characters come at address ADDRESS and the data in STREAM follow.  */
  338.  
  339. static void
  340. print_strings (filename, stream, address, stop_point, magiccount, magic)
  341.      char *filename;
  342.      FILE *stream;
  343.      file_ptr address;
  344.      int stop_point;
  345.      int magiccount;
  346.      char *magic;
  347. {
  348.   int bufsize = 100;
  349.   char *buf = (char *) xmalloc (bufsize);
  350.  
  351.   while (1)
  352.     {
  353.       int i;
  354.       int c;
  355.  
  356.       /* See if the next `string_min' chars are all graphic chars.  */
  357.     tryline:
  358.       if (stop_point && address >= stop_point)
  359.     break;
  360.       for (i = 0; i < string_min; i++)
  361.     {
  362.       if (magiccount)
  363.         {
  364.           magiccount--;
  365.           c = *magic++;
  366.         }
  367.       else
  368.         {
  369.           if (stream == NULL)
  370.         return;
  371.           c = getc (stream);
  372.           if (c < 0)
  373.         return;
  374.         }
  375.       address++;
  376.       if (!isgraphic (c))
  377.         /* Found a non-graphic.  Try again starting with next char.  */
  378.         goto tryline;
  379.       buf[i] = c;
  380.     }
  381.  
  382.       /* We found a run of `string_min' graphic characters.
  383.      Now see if it is terminated with a NUL byte or a newline.   */
  384.       while (1)
  385.     {
  386.       if (i == bufsize)
  387.         {
  388.           bufsize *= 2;
  389.           buf = (char *) xrealloc (buf, bufsize);
  390.         }
  391.       if (magiccount)
  392.         {
  393.           magiccount--;
  394.           c = *magic++;
  395.         }
  396.       else
  397.         {
  398.           if (stream == NULL)
  399.         return;
  400.           c = getc (stream);
  401.           if (c < 0)
  402.         return;
  403.         }
  404.       address++;
  405.       if (c == '\0' || c == '\n')
  406.         break;        /* It is; print this string.  */
  407.       if (!isgraphic (c))
  408.         goto tryline;    /* It isn't; give up on this string.  */
  409.       buf[i++] = c;        /* The string continues; store it all.  */
  410.     }
  411.  
  412.       /* If we get here, the string is all graphics and properly terminated,
  413.      so print it.  It is all in `buf' and `i' is its length.  */
  414.       buf[i] = '\0';
  415.       if (print_filenames)
  416.     printf ("%s: ", filename);
  417.       if (print_addresses)
  418.     switch (address_radix)
  419.       {
  420.       case 8:
  421.         printf ("%7lo ", (unsigned long) (address - i - 1));
  422.         break;
  423.  
  424.       case 10:
  425.         printf ("%7ld ", (long) (address - i - 1));
  426.         break;
  427.  
  428.       case 16:
  429.         printf ("%7lx ", (unsigned long) (address - i - 1));
  430.         break;
  431.       }
  432.  
  433.       for (i = 0; (c = buf[i]) != '\0'; i++)
  434.     switch (c)
  435.       {
  436.       case '\n':
  437.         printf ("\\n");
  438.         break;
  439.       case '\t':
  440.         printf ("\\t");
  441.         break;
  442.       case '\f':
  443.         printf ("\\f");
  444.         break;
  445.       case '\b':
  446.         printf ("\\b");
  447.         break;
  448.       case '\r':
  449.         printf ("\\r");
  450.         break;
  451.       default:
  452.         putchar (c);
  453.       }
  454.       putchar ('\n');
  455.     }
  456. }
  457.  
  458. /* Parse string S as an integer, using decimal radix by default,
  459.    but allowing octal and hex numbers as in C.  */
  460.  
  461. static int
  462. integer_arg (s)
  463.      char *s;
  464. {
  465.   int value;
  466.   int radix = 10;
  467.   char *p = s;
  468.   int c;
  469.  
  470.   if (*p != '0')
  471.     radix = 10;
  472.   else if (*++p == 'x')
  473.     {
  474.       radix = 16;
  475.       p++;
  476.     }
  477.   else
  478.     radix = 8;
  479.  
  480.   value = 0;
  481.   while (((c = *p++) >= '0' && c <= '9')
  482.      || (radix == 16 && (c & ~40) >= 'A' && (c & ~40) <= 'Z'))
  483.     {
  484.       value *= radix;
  485.       if (c >= '0' && c <= '9')
  486.     value += c - '0';
  487.       else
  488.     value += (c & ~40) - 'A';
  489.     }
  490.  
  491.   if (c == 'b')
  492.     value *= 512;
  493.   else if (c == 'B')
  494.     value *= 1024;
  495.   else
  496.     p--;
  497.  
  498.   if (*p)
  499.     {
  500.       fprintf (stderr, "%s: invalid integer argument %s\n", program_name, s);
  501.       exit (1);
  502.     }
  503.   return value;
  504. }
  505.  
  506. static void
  507. usage (stream, status)
  508.      FILE *stream;
  509.      int status;
  510. {
  511.   fprintf (stream, "\
  512. Usage: %s [-afov] [-n min-len] [-min-len] [-t {o,x,d}] [-]\n\
  513.        [--all] [--print-file-name] [--bytes=min-len] [--radix={o,x,d}]\n\
  514.        [--target=bfdname] [--help] [--version] file...\n",
  515.        program_name);
  516.   exit (status);
  517. }
  518.